<template>
  <!-- 特性项检测报警配置表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="500px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160" v-show="!props.isReadonly">
      <el-row>
        <el-col :span="24">
          <el-form-item label="产品" prop="productId">
            <el-select
              v-model="formData.productId"
              style="width: 100%"
              placeholder="请选择产品型号"
              clearable
              filterable
              @change="productChange"
            >
              <el-option
                v-for="item in productList"
                :key="item.id"
                :label="item.productCode"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="工序" prop="processId">
            <el-select
              v-model="formData.processId"
              placeholder="请选择工序"
              clearable
              style="width: 100%"
              @change="processChange"
              filterable
            >
              <el-option
                v-for="item in processList"
                :key="item.id"
                :label="item.processName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="特性" prop="paramId">
            <el-select
              v-model="formData.paramId"
              placeholder="请选择特性"
              clearable
              style="width: 100%"
              @change="paramChange"
              filterable
            >
              <el-option
                v-for="item in paramList"
                :key="item.paramId"
                :label="item.paramName"
                :value="item.paramId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="偏差百分比" prop="diffRate">
            <el-input-number
              v-model="formData.diffRate"
              placeholder="请输入偏差百分比"
              clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="跳动阈值百分比" prop="jumpRate">
            <el-input-number
              v-model="formData.jumpRate"
              placeholder="请输入跳动阈值百分比"
              clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="跳动预警对比数据个数" prop="jumpNumber">
            <el-input-number
              v-model="formData.jumpNumber"
              placeholder="请输入跳动预警对比数据个数"
              :precision="0"
              clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否启用" prop="enabled">
            <el-radio-group
              v-model="formData.enabled"
            >
              <el-radio label="1">启用</el-radio>
              <el-radio label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-descriptions
      class="margin-top"
      :column="1"
      size="large"
      border
      v-show="props.isReadonly"
    >
      <el-descriptions-item label="产品">
        <template #label>
          <div class="cell-item">产品</div>
        </template>
        {{ formData.productCode }}
      </el-descriptions-item>
      <el-descriptions-item label="工序">
        {{ formData.processCode }}
      </el-descriptions-item>
      <el-descriptions-item label="特性">
        {{ formData.paramName }}
      </el-descriptions-item>
      <el-descriptions-item label="偏差百分比">
        {{ formData.diffRate }}
      </el-descriptions-item>
      <el-descriptions-item label="跳动阈值百分比">
        {{ formData.jumpRate }}
      </el-descriptions-item>
      <el-descriptions-item label="跳动预警对比数据个数">
        {{ formData.jumpNumber }}
      </el-descriptions-item>
      <el-descriptions-item label="是否启用">
        {{ formData.enabled == 1 ? "启用" : "停用" }}
      </el-descriptions-item>
    </el-descriptions>

    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!props.isReadonly" type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import {useDictStoreHook} from "@/store/modules/dictionary";
import {useDictionary} from "@/utils/useTable"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "ETLQDFEATUREALARMCONFIGADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  productId: [
    {
      required: true,
      message: "请选择产品",
      trigger: "change",
    },
  ],
  productCode: [],
  productName: [],
  processId: [
    {
      required: true,
      message: "请选择工序",
      trigger: "change",
    },
  ],
  processCode: [],
  processName: [],
  paramId: [
    {
      required: true,
      message: "请选择特性",
      trigger: "change",
    },
  ],
  paramCode: [],
  paramName: [],
  diffRate: [],
  jumpRate: [],
  jumpNumber: [],
  enabled: [],
  deleted: [],
  createTime: [],
  createdUserId: [],
  createdUserName: [],
  lastUpdatedUserId: [],
  lastUpdatedUserName: [],
  lastUpdateTime: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  productId: "",
  productCode: "",
  productName: "",
  processId: "",
  processCode: "",
  processName: "",
  paramId: "",
  paramCode: "",
  paramName: "",
  diffRate: "",
  jumpRate: "",
  jumpNumber: "",
  enabled: "1",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  lastUpdateTime: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = ""
    if (newVal?.id) {
      const {
        id,
        productId,
        productCode,
        productName,
        processId,
        processCode,
        processName,
        paramId,
        paramCode,
        paramName,
        diffRate,
        jumpRate,
        jumpNumber,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
      } = newVal;
      Object.assign(formData, {
        id,
        productId,
        productCode,
        productName,
        processId,
        processCode,
        processName,
        paramId,
        paramCode,
        paramName,
        diffRate,
        jumpRate,
        jumpNumber,
        enabled,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdatedUserId,
        lastUpdatedUserName,
        lastUpdateTime,
      });
      getParamList();
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = ""
  formData.productId = ""
  formData.productCode = ""
  formData.productName = ""
  formData.processId = ""
  formData.processCode = ""
  formData.processName = ""
  formData.paramId = ""
  formData.paramCode = ""
  formData.paramName = ""
  formData.diffRate = ""
  formData.jumpRate = ""
  formData.jumpNumber = ""
  formData.enabled = "1"
  formData.deleted = ""
  formData.createTime = ""
  formData.createdUserId = ""
  formData.createdUserName = ""
  formData.lastUpdatedUserId = ""
  formData.lastUpdatedUserName = ""
  formData.lastUpdateTime = ""
}

const loading = ref(false);

/** 特性项检测报警配置表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/pqd-etl/qdfeaturealarmconfig/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}


// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

//产品列表
const productList = ref([]);
const getProductList = async () => {
  const {data} = await request({
    url: "mdm/mdmproductinfo/getList",
    method: "post",
    data: {},
  });
  productList.value = data;
};
const productChange = (productId) => {
  const find = productList.value.find((item) => item.id === productId);
  formData.productCode = find.productCode;
  formData.productName = find.productName;
  getParamList()
}

const processList: any = ref([]);

async function getProcessList() {
  const {data} = await request({
    url: `/mdm/mdmprocess/getList`,
    method: "post",
    data: {},
  });
  processList.value = data;
}

const processChange = (processId) => {
  const find = processList.value.find((item) => item.id === processId);
  formData.processCode = find.processCode;
  formData.processName = find.processName;
  getParamList()
}

const paramList = ref<any>([]);

async function getParamList() {
  if (formData.productId && formData.processId) {
    const {data} = await request({
      url: "/dmpDosPcm/pcmmeasurefeatures/getAllPropertyList",
      method: "post",
      data: {
        modelId: formData.productId,
        processId: formData.processId,
      },
    });
    paramList.value = data;
  } else paramList.value = [];
}


const paramChange = (paramId) => {
  const find = paramList.value.find((item) => item.paramId === paramId);
  formData.paramCode = find.paramCode;
  formData.paramName = find.paramName;
  getParamList()
}

onMounted(() => {
  getProductList();
  getProcessList();
});

</script>
